<?php

$thisPage = "popup_productsearch";

TemplateHelper::uselayout("popup");

addDataService("geths2");
addDataService("geths4");
addHtmlService("search");

$buttons = array();
$buttons[] = TemplateHelper::addButton("Search products", "submitSearch(this)");
$buttons[] = TemplateHelper::addButton("Close popup", "closePopup()");

function executeService($serviceName, $params) {
    Log::debug("service name: $serviceName");
    if ($serviceName == "geths2") {
        return execute_geths2($params);
    } else if ($serviceName == "geths4") {
        return execute_geths4($params);
    } else if ($serviceName == "search") {
        return execute_search($params);
    }
}

function execute_geths2($params) {
    Database::loadModel("hs2");
    $result = Database::getAll();
    return serviceSuccessful(array(
        items => $result
    ));
}

function execute_geths4($params) {
    Database::loadModel("hscodes");
    $result = Database::getModel()->getHS4ByHS2code($params['hs2']);
    return serviceSuccessful(array(
        items => $result
    ));
}

function execute_search($params) {
    setPopupCloseCallback("startSearch", $params);
    addPopupResult("submitparams", $params);
    return generatePopupResult();
}

function buildLayout(&$smarty) {
    global $thisPage;
    global $buttons;
    global $popupId;

    $smarty->assign("content", $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "productsearch")));
    $smarty->assign("buttons", $buttons);
}

function getPageHeader() {
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    return TemplateHelper::getDefaultPageFooter();
}

?>
